home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / APW.SC / SC19ActionNDA / ActionNDA.p < prev    next >
Encoding:
Text File  |  1990-06-20  |  15.2 KB  |  485 lines  |  [TEXT/pdos]

  1. UNIT ActionNDAv2;
  2.  
  3.  {  ActionNDA -- Version 3.0 
  4.     This NDA reports all action codes passed to the NDA action routine
  5.     (DAAction).  This is a useful test for applications that want to be NDA
  6.     friendly.
  7.  
  8.     The runAction indicator changes state every time a runAction code is passed
  9.     to DAAction.  A value of 0 is used for the Period field of the NDA header
  10.     so this will occur as often as possible (every time the application calls
  11.     SystemTask).  If the runAction indicator does not blink, then your
  12.     application is not calling SystemTask (applications using TaskMaster don't
  13.     need to worry about this - TaskMaster calls SystemTask for you).
  14.  
  15.     The cursorAction indicator changes state every time a cursorAction code is
  16.     passed to DAAction.  There are two differences between cursorAction and
  17.     runAction:
  18.       1)  cursorActions occur when only when the NDA is the frontmost
  19.           window.  runActions always occur if the NDA is open.
  20.       2)  cursorActions occur everytime SystemTask is called (if the NDA is
  21.           the frontmost window.)  runActions occur when SystemTask is called
  22.           only if the time period specified by the NDA header's Period field
  23.           has elapsed.  cursorActions allow NDAs the opportunity to change
  24.           the cursor when it is over the NDA's window.
  25.  
  26.     Whenever an editAction code is passed to DAAction (this will happen when
  27.     Undo, Cut, Copy, Paste, or Clear is selected from the Edit menu), the type
  28.     of editAction is displayed by the NDA for approximately 2 seconds.  If the
  29.     editAction is not displayed, possible problems in your application are:
  30.       o You didn't use the reserved ID numbers (250 through 255) for the
  31.         Undo, Cut, Copy, Paste, Clear and Close menu item ID numbers.
  32.       o Your application does not use TaskMaster and you are not calling
  33.         SystemEdit when the Undo, Cut, Copy, Paste, or Clear menu items
  34.         are selected when a system window (NDA) is the frontmost window.
  35.         Remember, the Undo, Cut, Copy, Paste, Clear and Close menu items
  36.         should always be enabled when a system window is the frontmost
  37.         window.
  38.         
  39.     Whenever an eventAction code is passed to DAAction, the type of contents
  40.     of the event record are displayed by the NDA.  There is a slight delay
  41.     after each event record is displayed, so you can see events that normally
  42.     flash by too fast to see.
  43.     Note: The mouseUpEvt eventAction is handled differently than other
  44.           eventActions.  Since mouseUpEvt eventActions can be passed to an
  45.           NDA when it is unsafe to draw in the NDA window (i.e., when you
  46.           release the mouse button after pulling down a menu over the NDA
  47.           window or when you release the mouse button after dragging the NDA
  48.           window), the event record for the mouseUpEvt is displayed the next
  49.           time a runAction occurs.  Apple IIGS Technical Note #71 tells you
  50.           to invalidate part of the window to cause an update event to happen
  51.           later.  Since this NDA is showing you the current event, that method
  52.           could not be used.
  53.     The events this NDA will see are mouseDownEvt, mouseUpEvt, keyDownEvt,
  54.     autoKeyEvt, updateEvt, and activateEvt.  If you aren't seeing all
  55.     of these events, possible problems in your application are:
  56.       o The taskMask you're passing to TaskMaster does not have the bits
  57.         set to pass those events to a system window (the NDA).
  58.       o Your application does not use TaskMaster and you are not calling
  59.         SystemClick when the event is in the NDAs window.
  60.     }
  61.  
  62. INTERFACE
  63.  
  64.   USES
  65.     Types,
  66.     IntMath,
  67.     MiscTool,
  68.     QuickDraw,
  69.     Events,
  70.     Controls,
  71.     Windows,
  72.     Desk;
  73.  
  74. {$Z+} { Export these procedures and functions. The identifiers for
  75.         these procedures and functions must not be changed! }
  76.  
  77.   FUNCTION NDAOpen: WindowPtr;
  78.   
  79.   PROCEDURE NDAClose;
  80.   
  81.   FUNCTION NDAAction(code: integer;
  82.                      evntRecPtr: EventRecordPtr): integer;
  83.  
  84.   PROCEDURE NDAInit(code: integer);
  85.  
  86. {$Z-} { keep any other procedures or functions local to this source }
  87.  
  88. IMPLEMENTATION
  89.  
  90.   CONST
  91.     titleStr = 'ActionNDA';   { the window title }
  92.  
  93.   VAR
  94.     { These variables are needed in every NDA. }
  95.     myWindOpen: boolean;      { true when NDA is open }
  96.     myWindParams: ParamList;  { window parameter list for NDA }
  97.     myNDATitle: Str255;       { variable for NDA window title }
  98.     myWindPtr: WindowPtr;     { pointer to NDA window }
  99.  
  100.     { These variables hold the current state of the NDA window. }
  101.     runState,                 { toggled on every runAction }
  102.     cursorState: boolean;     { toggled on every cursorAction }
  103.     editState: integer;       { the last edit Action code received }
  104.     eventState: EventRecord;  { a copy of the last event record passed }
  105.     drawMouseUp: boolean;     { tells DrawRunAction to handle mouseUpEvts }
  106.     editCounter: integer;       { Decremented on every runAction until 0. }
  107.                               { At 0, the editAction is erased and      }
  108.                               { editCounter is set to -1. }
  109.  
  110.  
  111. { Declare the following procedures (specific to this NDA) FORWARD. }
  112.  
  113.   PROCEDURE DrawEventAction;
  114.     FORWARD;
  115.  
  116.   PROCEDURE DrawRunAction;
  117.     FORWARD;
  118.  
  119.   PROCEDURE DrawCursorAction;
  120.     FORWARD;
  121.  
  122.   PROCEDURE DrawEditAction;
  123.     FORWARD;
  124.  
  125.   PROCEDURE DrawWindowText;
  126.     FORWARD;
  127.  
  128.  
  129. { The next 4 procedures and functions are in every NDA. }
  130.  
  131.  
  132.   FUNCTION NDAOpen: WindowPtr;
  133.  
  134.     BEGIN
  135.       IF myWindOpen
  136.         THEN
  137.           SelectWindow(myWindPtr) { NDA window is already open, so select it }
  138.         ELSE
  139.           BEGIN   { NDA window is not open }
  140.             { init some of the global variables }
  141.             drawMouseUp := False;
  142.             editCounter := 0;
  143.             editState := 0; { something that won't be drawn }
  144.             myNDATitle := 'ActionNDA';
  145.             WITH myWindParams DO  { init myWindParams }
  146.               BEGIN
  147.                 paramLength := sizeof(ParamList);
  148.                 wFrameBits := fTitle+fClose+fMove+fVis;
  149.                 wTitle := @myNDATitle;
  150.                 wRefCon := 0;
  151.                 SetRect(wZoom,0,0,0,0);
  152.                 wColor := NIL;
  153.                 wYOrigin := 0;
  154.                 wXOrigin := 0;
  155.                 wDataH := 0;
  156.                 wDataW := 0;
  157.                 wMaxH := 0;
  158.                 wMaxW := 0;
  159.                 wScrollVer := 0;
  160.                 wScrollHor := 0;
  161.                 wPageVer := 0;
  162.                 wPageHor := 0;
  163.                 wInfoRefCon := 0;
  164.                 wInfoHeight := 0;
  165.                 wFrameDefProc := NIL;
  166.                 wInfoDefProc := NIL;
  167.                 wContDefProc := NIL;
  168.                 SetRect(wPosition, 40, 35, 275, 155);
  169.                 wPlane := WindowPtr(-1);
  170.                 wStorage := NIL;
  171.               END;
  172.             myWindPtr := NewWindow(myWindParams); { create the NDA window }
  173.             SetSysWindow(myWindPtr);  { and make it a system window }
  174.           END;
  175.       NDAOpen := myWindPtr; { result of function = pointer to NDA window }
  176.       myWindOpen := true; { tell rest of NDA that we're open }
  177.     END; { of NDAOpen }
  178.  
  179.  
  180.   PROCEDURE NDAClose;
  181.  
  182.     BEGIN
  183.       CloseWindow(myWindPtr); { close the NDA window }
  184.       myWindOpen := False;  { tell rest of NDA that we're closed }
  185.     END; { of NDAClose }
  186.  
  187.  
  188.   FUNCTION NDAAction(code: integer;
  189.                      evntRecPtr: EventRecordPtr): integer;
  190.  
  191.     VAR
  192.       currPort: GrafPortPtr;
  193.       r: rect;
  194.  
  195.     BEGIN
  196.       NDAAction := 0; { default to result of 0 }
  197.       CASE code OF
  198.         eventAction:  { handle the eventAction code }
  199.           BEGIN
  200.             eventState := evntRecPtr^;
  201.             CASE evntRecPtr^.what OF
  202.               mouseDownEvt, keyDownEvt, autoKeyEvt: DrawEventAction;
  203.               mouseUpEvt:
  204.                 BEGIN
  205.                   drawMouseUp := true;
  206.                 END;
  207.               updateEvt:
  208.                 BEGIN
  209.                   currPort := GetPort;
  210.                   SetPort(myWindPtr);
  211.                   SetRect(r, 0, 0, 1000, 1000);
  212.                   InvalRect(r); { for this NDA, redraw everything }
  213.                   BeginUpdate(myWindPtr);
  214.                   DrawWindowText; { redraw the content of the NDA window }
  215.                   DrawRunAction;
  216.                   DrawCursorAction;
  217.                   DrawEventAction;
  218.                   IF editCounter > 0
  219.                     THEN DrawEditAction;
  220.                   EndUpdate(myWindPtr);
  221.                   SetPort(currPort);
  222.                 END; { updateEvt }
  223.               activateEvt: DrawEventAction;
  224.             END; { case }
  225.           END;
  226.         runAction:  { handle the runAction code }
  227.           BEGIN
  228.             runState := NOT runState;
  229.             DrawRunAction;
  230.           END;
  231.         cursorAction: { handle the cursorAction code }
  232.           BEGIN
  233.             cursorState := NOT cursorState;
  234.             DrawCursorAction;
  235.           END;
  236.         undoAction, cutAction, copyAction, pasteAction, clearAction:
  237.                     { handle the edit action codes }
  238.           BEGIN
  239.             editState := Code;
  240.             DrawEditAction;
  241.             NDAAction := 1; { yes, we handled the edit }
  242.           END;
  243.       END;
  244.     END; { of NDAAction }
  245.  
  246.  
  247.   PROCEDURE NDAInit(code: integer);
  248.  
  249.     BEGIN
  250.       IF code = 0
  251.         THEN
  252.           BEGIN
  253.             { A DeskShutDown Call, close window if it's open }
  254.             IF myWindOpen
  255.               THEN NDAClose;
  256.           END
  257.         ELSE  { A DeskStartUp Call, init myWindOpen flag }
  258.           myWindOpen := false;
  259.     END; { of NDAInit }
  260.  
  261.  
  262. { The following procedures are specific to this NDA. }
  263.  
  264.  
  265.   PROCEDURE DrawEventAction;
  266.   { This procedure displays the contents of the eventRecord stored in
  267.     eventState whenever an eventAction code is received. }
  268.  
  269.     VAR
  270.       aStr: Str255;
  271.       currPort: GrafPortPtr;
  272.       r: rect;
  273.       aWord, I: integer;
  274.       startTick: Longint;
  275.  
  276.     BEGIN
  277.       currPort := GetPort;
  278.       SetPort(myWindPtr);
  279.       SetRect(r, 82, 40, 1000, 90);
  280.       EraseRect(r);
  281.  
  282.       MoveTo(82, 50); { display EventRecord.what }
  283.       CASE eventState.what OF
  284.         mouseDownEvt: DrawString('mouseDownEvt');
  285.         mouseUpEvt: DrawString('mouseUpEvt');
  286.         keyDownEvt: DrawString('keyDownEvt');
  287.         autoKeyEvt: DrawString('autoKeyEvt');
  288.         updateEvt: DrawString('updateEvt');
  289.         activateEvt: DrawString('activateEvt');
  290.       END;
  291.  
  292.       MoveTo(82, 60); { display EventRecord.message }
  293.       CASE eventState.what OF
  294.         mouseDownEvt, mouseUpEvt:
  295.           BEGIN
  296.             aStr := 'button x';
  297.             Long2Hex(eventState.message, pointer(Longint(@aStr) + 8), 1);
  298.             DrawString(aStr);
  299.           END;
  300.         keyDownEvt, autoKeyEvt:
  301.           BEGIN
  302.             aStr := 'ASCII $xx';
  303.             Long2Hex(eventState.message, pointer(Longint(@aStr) + 8), 2);
  304.             DrawString(aStr);
  305.           END;
  306.         updateEvt, activateEvt:
  307.           BEGIN
  308.             aStr := 'WindowPtr($xx/xxxx)';
  309.             Long2Hex(HiWord(eventState.message),
  310.                      pointer(Longint(@aStr) + 12), 2);
  311.             Long2Hex(LoWord(eventState.message),
  312.                      pointer(Longint(@aStr) + 15), 4);
  313.             DrawString(aStr);
  314.           END;
  315.       END;
  316.  
  317.       MoveTo(82, 70); { display EventRecord.when }
  318.       aStr := '$xxxxxxxx';
  319.       Long2Hex(eventState.when, pointer(Longint(@aStr) + 2), 8);
  320.       DrawString(aStr);
  321.  
  322.       MoveTo(82, 80); { display EventRecord.where }
  323.       aStr := '($xxxx, $xxxx)';
  324.       Long2Hex(HiWord(Longint(eventState.where)),
  325.                pointer(Longint(@aStr) + 3), 4);
  326.       Long2Hex(LoWord(Longint(eventState.where)),
  327.                pointer(Longint(@aStr) + 10), 4);
  328.       DrawString(aStr);
  329.  
  330.       MoveTo(82, 90); { display EventRecord.modifiers }
  331.       aStr := '%';
  332.       aWord := $8000;
  333.       FOR I := 1 TO 16 DO { convert modifiers to a binary string }
  334.         BEGIN
  335.           IF I = 9
  336.             THEN aStr := Concat(aStr, ' ');
  337.           IF BAnd(aWord, eventState.modifiers) <> 0
  338.             THEN aStr := Concat(aStr, '1')
  339.             ELSE aStr := Concat(aStr, '0');
  340.           aWord := BSR(aWord,1);
  341.         END;
  342.       DrawString(aStr);
  343.  
  344.       SetPort(currPort);  { pause }
  345.       startTick := GetTick;
  346.       REPEAT
  347.       UNTIL (GetTick > startTick + 10);
  348.     END; { of DrawEditAction }
  349.  
  350.  
  351.   PROCEDURE DrawRunAction;
  352.   { This procedure paints or erases the runAction indicator whenever
  353.     a runAction code is received.  It also erases the text for the
  354.     last editAction received when editCounter reaches 0, and it displays
  355.     the eventRecord for mouseUp events if the drawMouseUp flag is true. }
  356.  
  357.     VAR
  358.       currPort: GrafPortPtr;
  359.       r: rect;
  360.  
  361.     BEGIN
  362.       IF drawMouseUp = true
  363.         THEN
  364.           BEGIN
  365.             DrawEventAction;
  366.             drawMouseUp := False;
  367.           END;
  368.  
  369.       currPort := GetPort;
  370.       SetPort(myWindPtr);
  371.  
  372.       IF editCounter > 0
  373.         THEN editCounter := pred(editCounter)
  374.         ELSE
  375.           IF editCounter = 0
  376.             THEN
  377.               BEGIN
  378.                 editCounter := pred(editCounter);
  379.                 SetRect(r, 90, 15, 150, 26);
  380.                 EraseRect(r);
  381.               END;
  382.  
  383.       SetRect(r, 86, 4, 94, 9);
  384.       IF runState
  385.         THEN PaintOval(r)
  386.         ELSE EraseOval(r);
  387.  
  388.       SetPort(currPort);
  389.     END; { of DrawRunAction }
  390.  
  391.  
  392.   PROCEDURE DrawCursorAction;
  393.   { This procedure paints or erases the cursorAction indicator whenever
  394.     a cursorAction code is received. }
  395.  
  396.     VAR
  397.       currPort: GrafPortPtr;
  398.       CurrColor: integer;
  399.       r: rect;
  400.  
  401.     BEGIN
  402.       currPort := GetPort;
  403.       SetPort(myWindPtr);
  404.       SetRect(r, 219, 4, 227, 9);
  405.       IF cursorState
  406.         THEN PaintOval(r)
  407.         ELSE EraseOval(r);
  408.       SetPort(currPort);
  409.     END; { of DrawCursorAction }
  410.  
  411.  
  412.   PROCEDURE DrawEditAction;
  413.   { This procedure displays the last edit Action code received.
  414.     It sets editCounter to 120.  editCounter is decremented whenever
  415.     a runAction code is received.  When editCounter reaches 0, the edit
  416.     Action type is erased in the NDA's window. }
  417.  
  418.     VAR
  419.       currPort: GrafPortPtr;
  420.  
  421.     BEGIN
  422.       currPort := GetPort;
  423.       SetPort(myWindPtr);
  424.       MoveTo(90, 25);
  425.       CASE editState OF
  426.         undoAction: DrawString('Undo   ');
  427.         cutAction: DrawString('Cut     ');
  428.         copyAction: DrawString('Copy   ');
  429.         pasteAction: DrawString('Paste ');
  430.         clearAction: DrawString('Clear ');
  431.       END;
  432.       SetPort(currPort);
  433.       editCounter := 120;
  434.     END; { of DrawEditAction }
  435.  
  436.  
  437.   PROCEDURE DrawWindowText;
  438.   { This procedure draws the static text items in the NDA window. }
  439.  
  440.     VAR
  441.       currPort: GrafPortPtr;
  442.       r: rect;
  443.  
  444.     BEGIN
  445.       currPort := GetPort;
  446.       SetPort(myWindPtr);
  447.  
  448.       MoveTo(5, 10);
  449.       DrawString('runAction:');
  450.       SetRect(r, 85, 3, 95, 10);
  451.       FrameOval(r);
  452.  
  453.       MoveTo(115, 10);
  454.       DrawString('cursorAction:');
  455.       SetRect(r, 218, 3, 228, 10);
  456.       FrameOval(r);
  457.  
  458.       MoveTo(5, 25);
  459.       DrawString('editAction:');
  460.  
  461.       MoveTo(5, 40);
  462.       DrawString('eventAction:');
  463.       MoveTo(39, 50);
  464.       DrawString('what=');
  465.       MoveTo(15, 60);
  466.       DrawString('message=');
  467.       MoveTo(39, 70);
  468.       DrawString('when=');
  469.       MoveTo(32, 80);
  470.       DrawString('where=');
  471.       MoveTo(5, 90);
  472.       DrawString('modifiers=');
  473.  
  474.       SetForeColor(1);
  475.       MoveTo(5, 105);
  476.       DrawString('Copyright 1990,');
  477.       MoveTo(5, 115);
  478.       DrawString('Apple Computer, Inc.');
  479.       SetForeColor(0);
  480.  
  481.       SetPort(currPort);
  482.     END; { of DrawWindowText }
  483.  
  484.   END.
  485.